        var sharedLinks = new Array();
        var viewerId;
         
        function loadViewer() {
			var req = opensocial.newDataRequest();
			req.add(req.newFetchPersonRequest("VIEWER"), 'viewer');
			req.send(fetchViewerHandler);
        }
        
        function fetchViewerHandler(resp) {
			var viewerResp = resp.get('viewer');  
			var viewer = viewerResp.getData();     
			viewerName = viewer.getDisplayName();
			viewerId = viewer.getId();
			displayViewerName('viewer', viewerName);    
		}
    
		function displayViewerName(insertId, viewerName){
			var html = '<p>' + viewerName + '</p>';
			document.getElementById(insertId).innerHTML = html;
		}

        function shareLink() {
          var shared = document.getElementById('share_links').value;
          var shared_title = document.getElementById('share_links_title').value;
          var link = {'url': shared, 'title' : shared_title };
          addToSharedLinks(link);
        }
        
        function addToSharedLinks(link){
			sharedLinks.push(link);
        }
    
		function fetchRequestForLinks(){
			var json = gadgets.json.stringify(sharedLinks);
			var req = opensocial.newDataRequest();
			req.add(req.newUpdatePersonAppDataRequest("VIEWER", 'links', json));
			var viewer = opensocial.newIdSpec({ "userId" : "VIEWER" });
			req.add(req.newFetchPersonAppDataRequest(viewer, 'links'), 'data');
			req.send(displaySharedLinks);
		}
    
		function displaySharedLinks(data){
			var html = '<h3>You have shared: </h3>';
			html += '<ul>';
			if(data != null){
				updateSharedLinks(data);
				for(i in sharedLinks){
					var link = sharedLinks[i];          
					var url = link['url'];
					var title = link['title'];
					html += '<li><a href=\"'+url+'\">'+title+'</a></li>';
				}
			}
			html += '</ul>';
			document.getElementById('shared').innerHTML = html;
		}
    
		function updateSharedLinks(data){
			var retrievedData = data.get('data').getData();
			var linksData = retrievedData[viewerId]['links'];
			linksData = gadgets.util.unescapeString(linksData);
			sharedLinks = gadgets.json.parse(linksData);
		}
        
        function init() {
			loadViewer();
			fetchRequestForLinks();          
        }

        gadgets.util.registerOnLoadHandler(init);
